<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryParamsRef" :inline="true" :model="queryParams" label-width="80px">
        <el-form-item label="厂商信息" prop="supplierName">
          <el-input v-model="queryParams.supplierName" clearable placeholder="请输入厂商"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="别名" prop="supplierJc">
          <el-input v-model="queryParams.supplierJc" clearable placeholder="请输入别名"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="是否国外" prop="foreignx">
          <el-radio-group v-model="queryParams.foreignx" @change="handleQuery">
            <el-radio value="">全部</el-radio>
            <el-radio v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue"
                      :value="dict.dictValue">{{
                dict.dictLabel
              }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否使用" prop="usedFlag" style="margin-left: 30px">
          <el-radio-group v-model="queryParams.usedFlag" @change="handleQuery">
            <el-radio value="">全部</el-radio>
            <el-radio v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue"
                      :value="dict.dictValue">{{
                dict.dictLabel
              }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <div class="mr20" style="float: right">
          <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
        </div>
      </el-form>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPerm="['base:drugSupplierCata:add']" plain type="primary" @click="handleAdd">
          <template
            #icon>
            <i-ep-plus/>
          </template>
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPerm="['base:drugSupplierCata:remove']" :disabled="multiple" plain type="danger"
                   @click="handleDelete">
          <template #icon>
            <i-ep-delete/>
          </template>
          删除
        </el-button>
        <el-button plain type="danger" @click="refreshCacheDrugSupplierCataFun">
          <template #icon>
            <i-ep-refresh/>
          </template>
          刷新缓存
        </el-button>
      </el-col>
      <!--            <el-col :span="1.5">-->
      <!--                <el-button-->
      <!--                    type="warning"-->
      <!--                    plain-->
      <!--                    icon="el-icon-download"-->
      <!--                    -->
      <!--                    @click="handleExport"-->
      <!--                    v-hasPerm="['base:drugSupplierCata:export']"-->
      <!--                >导出-->
      <!--                </el-button>-->
      <!--            </el-col>-->
    </el-row>

    <el-tabs v-model="tabsValue" type="border-card" @tab-click="handleTabsEdit">
      <el-tab-pane label="生产商" name="0">
      </el-tab-pane>
      <el-tab-pane label="供应商" name="1">
      </el-tab-pane>
      <tab-table ref="tabTableRef" :query-params="queryParams" @handleSelectionChange="handleSelectionChange"
                 @handleUpdate="handleUpdate"/>
    </el-tabs>

    <drug-supplier-cata-add ref="drugSupplierCataAddRef" :supplier-class="queryParams.supplierClass"
                            :tabs-name="tabsName" :title="title" @getList="getList"/>
  </div>
</template>

<script lang="ts" setup>
import {delDrugSupplierCata, refreshCacheDrugSupplierCata} from "@/api/drug/drugSupplierCata";
import {useDictStoreHook} from "@/store/modules/dict";
import tabTable from "@/views/drug/drugSupplierCata/tabTable.vue";
import drugSupplierCataAdd from "@/views/drug/drugSupplierCata/drugSupplierCataAdd.vue";

const dictStore = useDictStoreHook();
const dictDataMap = ref(dictStore.isTypeGetData('sys_yes_no'))
const tabTableRef = ref()
const drugSupplierCataAddRef = ref()
// tabs 标签页默认值
const tabsValue = ref('0')
const tabsName = ref('生产商')
// 显示搜索条件
const showSearch = ref(true)
// 非多个禁用
const multiple = ref(true)
// 弹出层标题
const title = ref("")
// 选中数组
const ids = ref([])
// 非单个禁用
const single = ref(true)
// 查询参数
const queryParamsRef = ref()
const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  supplierJc: undefined,
  supplierName: undefined,
  supplierClass: "0",
  inputCode: undefined,
  trademark: undefined,
  foreignx: '',
  supplierCode: undefined,
  usedFlag: '',
  remarks: undefined
})
queryParams.value.supplierClass = tabsValue.value
getList();

/** 新增按钮操作 */
function handleAdd() {
  title.value = `添加药品${tabsName.value}维护`;
  drugSupplierCataAddRef.value.handleAdd()
}

/** 修改按钮操作 */
function handleUpdate(row) {
  title.value = `修改药品${tabsName.value}维护`;
  drugSupplierCataAddRef.value.handleUpdate(row)
}

function handleTabsEdit(tab, _event) {
  queryParams.value.supplierClass = toRaw(tab).index.value
  tabsName.value = toRaw(tab).props.label
  getList()
}

function getList() {
  nextTick(() => {
    tabTableRef.value.getList()
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields()
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/** 删除按钮操作 */
function handleDelete(row) {
  const idsThis = row.id || ids.value;
  ElMessageBox.confirm(`是否确认删除此的数据项？`,
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    delDrugSupplierCata(idsThis).then(() => {
      getList();
      ElMessage.success("删除成功");
    })
  }).catch(() => {
  })
}

/** 导出按钮操作 */
function handleExport() {
  // download('base/drugSupplierCata/export', {
  //     ...queryParams
  // }, `drugSupplierCata_${new Date().getTime()}.xlsx`)
}

function refreshCacheDrugSupplierCataFun() {
  refreshCacheDrugSupplierCata().then((res: any) => {
    ElMessage.success(res.msg);
  })
}
</script>


<script lang="ts">
export default {
  name: "DrugSupplierCata"
}
</script>

<style scoped>
:deep(.el-tabs__content) {
  padding-bottom: 40px;
}

:deep(.el-form-item__content) {
  margin-bottom: -1px;
}
</style>
